#include "mainwindow.h"
#include "videorenderwidget.h"
#include "arrowrectangle.h"

#include <QApplication>
#include <QMenu>
#include <QTime>
#include <QDebug>
#include <QHBoxLayout>
#include <QFileDialog>
#include <QMenuBar>
#include <QPushButton>
#include <QLabel>
#include <QTimer>

MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
{
    setWindowIcon(QIcon(":/images/hl_player_48px.svg"));
    setWindowTitle(qApp->applicationName());

    m_arrowRectangle = new ArrowRectangle(this);

    QMenu* pDMenu = menuBar()->addMenu(tr("File"));
    QAction* action(new QAction(tr("Help"), this));
    QAction* openAction(new QAction(tr("Open"), this));
    pDMenu->addAction(openAction);
    pDMenu->addAction(action);
    connect(openAction, &QAction::triggered, this, &MainWindow::openFile);

    m_videoRenderWidget = new VideoRenderWidget(this);
    setCentralWidget(m_videoRenderWidget);

    initFloatWidget();
    initContent();
    initConnection();
}

MainWindow::~MainWindow()
{
}

void MainWindow::initContent()
{
}

void MainWindow::initFloatWidget()
{
    m_floatingWidget = new QFrame(this);
    QHBoxLayout* layout = new QHBoxLayout;
    m_floatingWidget->setLayout(layout);

    m_buttonBox = new QWidget(this);
    QHBoxLayout* hlayout = new QHBoxLayout(m_buttonBox);
    m_buttonBox->setFixedHeight(50);
    QPushButton* preButton = new QPushButton(QIcon::fromTheme(":/images/before.svg"), "", this);
    preButton->setIconSize(QSize(15, 15));
    QPushButton* playButton = new QPushButton(QIcon::fromTheme(":/images/play.svg"), "", this);
    playButton->setIconSize(QSize(15, 15));
    QPushButton* nextButton = new QPushButton(QIcon::fromTheme(":/images/next.svg"), "", this);
    nextButton->setIconSize(QSize(15, 15));
    hlayout->addWidget(preButton);
    hlayout->addWidget(playButton);
    hlayout->addWidget(nextButton);

    m_label = new QLabel("00:00:00", this);
    m_slider = new QSlider(Qt::Horizontal, this);
    m_totalLabel = new QLabel("00:00:00", this);

    QPushButton* fullButton = new QPushButton(this);
    fullButton->setFixedSize(50, 50);
    fullButton->setIcon(QIcon::fromTheme(":/images/fullsreen.svg"));
    fullButton->setIconSize(QSize(20, 20));
    connect(fullButton, &QPushButton::clicked, [ = ]()
    {
        if (this->isFullScreen())
        {
            this->showNormal();
            fullButton->setIcon(QIcon::fromTheme(":/images/fullsreen.svg"));
        }
        else
        {
            this->showFullScreen();
            fullButton->setIcon(QIcon::fromTheme(":/images/quit.svg"));
        }
    });

    m_volumeButton = new QPushButton(this);
    m_volumeButton->setFixedSize(50, 50);
    m_volumeButton->setIcon(QIcon::fromTheme(":/images/volume.svg"));
    m_volumeButton->setIconSize(QSize(20, 20));

    QPushButton* listButton = new QPushButton(this);
    listButton->setFixedSize(50, 50);
    listButton->setIcon(QIcon::fromTheme(":/images/list.svg"));
    listButton->setIconSize(QSize(20, 20));

    layout->addWidget(m_buttonBox);
    layout->addWidget(m_label);
    layout->addWidget(m_slider);
    layout->addWidget(m_totalLabel);
    layout->addWidget(fullButton);
    layout->addWidget(m_volumeButton);
    layout->addWidget(listButton);
}

void MainWindow::initConnection()
{
    connect(m_volumeButton, &QPushButton::clicked, [ = ]()
    {
        m_arrowRectangle->setVisible(true);
        m_arrowRectangle->raise();
        QPoint point = m_floatingWidget->geometry().topLeft() + m_volumeButton->geometry().topLeft();
        m_arrowRectangle->move(point.x() + 30, point.y() - 8);
        QTimer::singleShot(3000, m_arrowRectangle, &ArrowRectangle::hide);
    });

    connect(&m_player, &QAVPlayer::audioFrame, this, &MainWindow::playAudio);

    connect(&m_player, &QAVPlayer::videoFrame, this, &MainWindow::playVideo);

    connect(&m_player, &QAVPlayer::durationChanged, this, [ = ](qint64 duration)
    {
        if (duration > 0)
        {
            auto text = formatTime(duration);
            m_totalLabel->setText(formatTime(duration));
            m_slider->setRange(0, duration);
        }
    });

    connect(&m_player, &QAVPlayer::played, this, [ = ](qint64 duration)
    {
        if (duration > 0)
        {
            auto text = formatTime(duration);
            m_label->setText(formatTime(duration));
            QSignalBlocker blocker(m_slider);
            m_slider->setValue(duration);
        }
    });
}

QString MainWindow::formatTime(int ms)
{
    QTime time(0, 0, 0);
    time = time.addMSecs(ms);
    return time.toString("hh:mm:ss");
}

void MainWindow::resizeEvent(QResizeEvent* event)
{
    m_floatingWidget->setFixedSize(this->width() - 20, 80);
    m_floatingWidget->move(10, this->height() - 90);
    m_arrowRectangle->setVisible(false);

    QMainWindow::resizeEvent(event);
}

void MainWindow::leaveEvent(QEvent* event)
{
    m_floatingWidget->setVisible(false);
    QMainWindow::leaveEvent(event);
}

void MainWindow::enterEvent(QEvent* event)
{
    m_floatingWidget->setVisible(true);
    QMainWindow::enterEvent(event);
}

void MainWindow::openFile()
{
    QString tempPath = QFileDialog::getOpenFileName(this, tr("open file"), "../", "*.*");

    if (!tempPath.isEmpty())
    {
        m_player.setSource(tempPath);
        m_player.play();
    }
}

void MainWindow::playAudio(const QAVAudioFrame &frame)
{
    m_audioOutput.play(frame);
}

void MainWindow::playVideo(const QAVVideoFrame &frame)
{
    QAVVideoFrame result = frame.convertTo(AV_PIX_FMT_YUV420P);
    m_videoRenderWidget->updateBuffer(result.size(), result.frame()->data[0], result.frame()->data[1], result.frame()->data[2],
                                      result.frame()->linesize[0], result.frame()->linesize[1], result.frame()->linesize[2]);
}
